﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    /// <summary>
    /// 车间数据层
    /// </summary>
    public class WorkShopService : DbHelperBase
    {
        /// <summary>
        /// 获取车间绑定下拉列表
        /// </summary>
        /// <returns></returns>
        public List<WorkShopEntity> GetWorkShopList()
        {
            var list = new List<WorkShopEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append("SELECT WSID,WSCode,WSName FROM UT_WorkShop where RecStatus=1 order by InputTime,WSCode");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new WorkShopEntity
                            {
                                WSCode =
                                    reader["WSCode"] == obj
                                        ? null
                                        : Convert.ToString(reader["WSCode"]),
                                WSName =
                                    reader["WSName"] == obj
                                        ? null
                                        : Convert.ToString(reader["WSName"])
                            };
                            if (reader["WSID"] != DBNull.Value)
                                model.WSID = new Guid(reader["WSID"].ToString());
                            list.Add(model);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加车间
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddWorkShop(WorkShopEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [UT_WorkShop]([WSID],[WSCode],[WSName],[WSDesc],[RecStatus]) ");
            strSql.Append(" VALUES(@WSID,@WSCode,@WSName,@WSDesc,@RecStatus) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@WSID", model.WSID);
                    Db.AddInParameter(cmd, "@WSCode", model.WSCode);
                    Db.AddInParameter(cmd, "@WSName", model.WSName);
                    Db.AddInParameter(cmd, "@WSDesc", model.WSDesc);
                    Db.AddInParameter(cmd, "@RecStatus", true);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 修改车间
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ModifyWorkShop(WorkShopEntity model)
        {

            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_WorkShop SET [WSCode] = @WSCode,[WSName] = @WSName,[WSDesc] = @WSDesc WHERE WSID = @WSID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@WSCode", model.WSCode);
                    Db.AddInParameter(cmd, "@WSName", model.WSName);
                    Db.AddInParameter(cmd, "@WSDesc", model.WSDesc);
                    Db.AddInParameter(cmd, "@WSID", model.WSID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        /// <summary>
        /// 获取车间单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WorkShopEntity GetWorkShopInfo(string id)
        {
            var model = new WorkShopEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [WSID],[WSCode],[WSName],[WSDesc],[RecStatus] FROM [UT_WorkShop] where WSID=@WSID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                Db.AddInParameter(cmd, "@WSID", id);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["WSID"] != DBNull.Value)
                                model.WSID = new Guid(reader["WSID"].ToString());
                            model.WSCode = reader["WSCode"] == obj ? null : Convert.ToString(reader["WSCode"]);
                            model.WSName = reader["WSName"] == obj ? null : Convert.ToString(reader["WSName"]);
                            model.WSDesc = reader["WSDesc"] == obj ? null : Convert.ToString(reader["WSDesc"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        /// <summary>
        /// 删除车间
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Remove(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_WorkShop Where WSID=@WSID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@WSID", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public int GetWorkShopCount(WorkShopQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_WorkShop AS A WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" and RecStatus =" + qcmodel.RecStatus);
            }
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        /// <summary>
        /// 获取车间列表分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public List<WorkShopEntity> GetWorkShopList(WorkShopQcEntity qcmodel)
        {
            var list = new List<WorkShopEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,[WSID],[WSCode],[WSName],[WSDesc],[RecStatus] FROM UT_WorkShop where 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.RecStatus))
            {
                strSql.Append(" and RecStatus =" + qcmodel.RecStatus);
            }
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new WorkShopEntity();
                            if (reader["WSID"] != DBNull.Value)
                                model.WSID = new Guid(reader["WSID"].ToString());
                            model.WSCode = reader["WSCode"] == obj ? null : Convert.ToString(reader["WSCode"]);
                            model.WSName = reader["WSName"] == obj ? null : Convert.ToString(reader["WSName"]);
                            model.WSDesc = reader["WSDesc"] == obj ? null : Convert.ToString(reader["WSDesc"]);
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToBoolean(reader["RecStatus"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        //public IList<WorkShopEntity> GetWorkShopListByBizDate(string bizDate)
        //{
        //    var list = new List<WorkShopEntity>();
        //    var strSql = new StringBuilder();
        //    object obj = DBNull.Value;
        //    strSql.Append(" SELECT DISTINCT C.WSID,C.WSCode,C.WSName FROM  UT_EqRunRec AS A  ");
        //    strSql.Append(" INNER JOIN UT_Equipment AS B ON A.EqID=B.EqID ");
        //    strSql.Append(" INNER JOIN UT_WorkShop AS C ON B.WSID=C.WSID ");
        //    strSql.Append(" WHERE A.EndTime IS NOT NULL AND C.RecStatus=1  AND A.BizDate=@BizDate ");
        //    using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
        //    {
        //        try
        //        {
        //            Db.AddInParameter(cmd, "@BizDate", bizDate);
        //            using (IDataReader reader = Db.ExecuteReader(cmd))
        //            {
        //                while (reader.Read())
        //                {
        //                    var model = new WorkShopEntity
        //                    {
        //                        WSCode =
        //                            reader["WSCode"] == obj
        //                                ? null
        //                                : Convert.ToString(reader["WSCode"]),
        //                        WSName =
        //                            reader["WSName"] == obj
        //                                ? null
        //                                : Convert.ToString(reader["WSName"])
        //                    };
        //                    if (reader["WSID"] != DBNull.Value)
        //                        model.WSID = new Guid(reader["WSID"].ToString());
        //                    list.Add(model);
        //                }
        //            }

        //        }
        //        catch (Exception ex)
        //        {
        //            EventsLog.WriteLog("ErrorInfo" + ex.Message);
        //        }
        //    }
        //    return list;
        //}
    }
}
